$(function() {

  if(class_id == '368'){
    $(".newsArticles .hd ul li").eq(0).addClass("on").siblings().removeClass("on");
  }
  if(class_id == '370'){
    $(".newsArticles .hd ul li").eq(1).addClass("on").siblings().removeClass("on");
  }
  if(class_id == '371'){
    $(".newsArticles .hd ul li").eq(2).addClass("on").siblings().removeClass("on");
  }

//滚动条拉到底部的时候去获取新的数据
  var stop=true; 
  var pageNumb = 2;

$(document).on("click",".loadMore",function(e){
    if(stop==true){ 
        stop=false;  
        scrollUrl();
    } 
    stopDefault(e);
  })
  
  
  //滚动调用的方法;
  function scrollUrl(){
    var url = $(".loadmore").attr("data-url");
    var index;
    var listli = $(".newsArticles .hd li");
    for(var i=0;i<listli.length;i++){
        if(listli[i].className == "on"){
            index = i;
            break;
        }
    }
    var cat_id = getUrlParam(cat_id);
    if(cat_id == null){
        getUrl(url+'?page='+pageNumb++,index);
    }else{
        getUrl(url+'&page='+pageNumb++,index);
    }
    stop=true;

  }


    getrecommend();
      //热文
      function getrecommend(){
        var url = jumpUrl+"/knowledge/most_liked?order=created_at&rows_per_page=3&class_id="+class_id;
        $.get(url,function(data){
            }, "json").error(function(data) {
                layer.msg("请求失败！")
            }).success(function(data) {
                var obj = data.data;
                console.info(obj)
                for(var i=0;i<obj.length; i++){
                    var time = obj[i].created_at;
                        var str2 = "";
                        var n = i+1
                        if(i+1 == obj.length){
                            str2+='<div class="list last">'
                        }else{
                            str2+='<div class="list">' 
                        }
                          str2+='<div class="mod-tit">'
                              str2+='<h2>推荐<span class="color2cb9ff">'+n+'</span></h2>'
                          str2+='</div>'
                          str2+='<h3 class="title">'
                            str2+='<a href="'+jumpUrl+'/knowledge/'+obj[i].id+'/view" title="'+obj[i].title+'">'+obj[i].title+'</a>'
                          str2+='</h3>'
                          str2+='<p class="info">'
                            str2+='<a target="_blank" href="javascript:void(0);" title="律团">律团</a>'
                            str2+='<span class="gap-point">'+time+'</span>'
                          str2+='</p>'
                          if (obj[i].info.length > 70) {
                            str2+='<p class="cont">'+obj[i].info.substr(0,70)+' ...</p>'
                          }else{
                            str2+='<p class="cont">'+obj[i].info+'</p>'
                          };
                          
                          str2+='<div class="options">'
                            str2+='<a href="javascript:void(0);" class="like js-like" title="点赞"  data-id="'+obj[i].id+'">'
                              str2+='<span class="icon"><i class="icon-like"></i></span>'
                              str2+='<span class="num">'+obj[i].likes_count+'</span>'
                            str2+='</a>'
                            str2+='<a class="comment" title="评价数"  data-id="'+obj[i].id+'">'
                              str2+='<span class="icon"><i class="icon-comment"></i></span>'
                              str2+='<span class="num">'+obj[i].comments_count+'</span>'
                            str2+='</a>'
                            str2+='<a href="javascript:void(0);" class="favorite" data-id="'+obj[i].id+'" title="收藏">'
                            if(obj[i].is_collect){
                                  str2+='<span class="icon"><i class="icon-favorite-on"></i></span>'
                            }else{
                                str2+='<span class="icon"><i class="icon-favorite"></i></span>'
                            }
                             
                              str2+='<span class="num shu">'+obj[i].collects_count+'</span>'
                            str2+='</a>'
                          str2+='</div>' 
                        str2+='</div>';
                        $(".newsArticles .column").append(str2);
                }
             })
      }
      
      //banner
      getbanner();
      function getbanner(){
        var url = jumpUrl+"/knowledge/latest";
        $.get(url,function(data){
            }, "json").error(function(data) {
                layer.msg("请求失败！")
            }).success(function(data) {
                var obj = data.data;
                console.info(obj);
                    var time = obj.created_at;
                    var title = obj.title;
                    if(title.length > 22){
                      title = title.substr(0,22)+'......';
                    }else{
                      title = title;
                    }
                    var str1=''
                        str1+='<h2><a href="'+jumpUrl+'/knowledge/'+obj.id+'/view" title="'+obj.title+'" class="title" title="'+title+'">'+title+'</a></h2>'
                        str1+='<div class="options">'
                            str1+='<a href="javascript:void(0);" class="like js-like" title="点赞"  data-id="'+obj.id+'">'
                              str1+='<span class="icon"><i class="icon-like"></i></span>'
                              str1+='<span class="num">'+obj.likes+'</span>'
                            str1+='</a>'
                            str1+='<a href="javascript:void(0);" class="comment" title="评价数"  data-id="'+obj.id+'">'
                              str1+='<span class="icon"><i class="icon-comment"></i></span>'
                              str1+='<span class="num">'+obj.comments+'</span>'
                            str1+='</a>'
                          str1+='</div>'
                        str1+='<div class="info">'+obj.created_at+'</div>'
                    $(".newsArticles .inner .post-cont").append(str1);

                    var str01 = ''
                        str01 += '<a target="_blank" href="" title="" class="pics">'
                          str01 += '<img alt="" src="'+obj.creator_avatar+'" width="80" height="80">'
                          str01 += '<i class="isAttest"></i>'
                        str01 += '</a>'
                        str01 += '<a target="_blank" href="'+jumpUrl+'/lawyer/'+obj.creator_id+'" title="" class="name">'+obj.creator_realname+'</a>'
                          if (obj.is_following) {
                          str01 += '<a href="##" data-id="'+obj.creator_id+'" class="unfollow" style="color:#fff;">取消关注</a>' 
                          }else{
                            str01 += '<a href="##" data-id="'+obj.creator_id+'" class="attention" style="color:#fff;">关    注</a>' 
                          }
                    $(".newsArticles .author .a-inner").append(str01);
             })
      }



    //点击获取 tabs 列表不同的信息
    jQuery(".newsSlide").slide({
        trigger:"click",
        delayTime:700,
        startFun:function( i,c,s )
        {  
            list_news(i,null);
        }

    });

    gethot();
      //一周热文
      function gethot(){
        var url = jumpUrl+"/knowledge/weekly?rows_per_page=8&class_id="+class_id;
        var loading = '<div class="loading">加载中...</div>'; 
        
        $(".sidebar .part .hot").append(loading);
        $.get(url,function(data){

            }, "json").error(function(data) {
                layer.msg("请求失败！")
            }).success(function(data) {
                $(".loading").remove();
                var obj = data.data;
                console.info(obj)
                var str = "";
                for(var i=0;i<obj.length; i++){
                    str+='<li>'
                        str+='<a href="'+jumpUrl+'/knowledge/'+obj[i].id+'/view" title="'+obj[i].title+'">'+obj[i].title+'</a>'
                    str+='</li>';
                }
                $(".sidebar .part .hot").append(str);
             })
      }



    //根据条件判断返回相对应的数据
    $(".articles .newsSortBy a").click(function(){
        var index;
        var $this = $(this);
        $this.addClass("active").siblings().removeClass("active");
        var thisId = $this.attr("data-id");
        var listli = $(".newsArticles .hd li");
        for(var i=0;i<listli.length;i++){
            if(listli[i].className == "on"){
                index = i;
                break;
            }
        }
        $(".newsArticles .bd ul:eq("+index+")").empty();
        $(".loadmore").remove();
        list_news(index,thisId);
    })
      //点击查看更多
      $(".more").click(function () {
            var height = $(".newsSortBy .title").height();
            if(height != 60){
              $(".newsSortBy .title").css("height",60)
              var height = 60;
            }else{
              $(".newsSortBy .title").css("height",30);
              var height = 30;
            };
          });


     /*看新闻 */
     function list_news(index,id){
        var url;
        if(index==0){ //看新闻
            if(id == null){
              $(".newsSortBy a").eq(0).addClass("active").siblings().removeClass("active");
              url = jumpUrl+"/news/list_news";
            }else
            {
                url = jumpUrl+"/news/list_news?cat_id="+id;
            }
        }
        if(index==1){ //搜案列
            if(id == null){
              $(".newsSortBy a").eq(0).addClass("active").siblings().removeClass("active");
              url = jumpUrl+"/case/list_case";
            }else
            {
                url = jumpUrl+"/case/list_case?cat_id="+id;
            }
            
        }
        if(index==2){ //学知识
            if(id == null){
              $(".newsSortBy a").eq(0).addClass("active").siblings().removeClass("active");
              url = jumpUrl+"/knowledge/article/list_articles";
            }else
            {
                url = jumpUrl+"/knowledge/article/list_articles?cat_id="+id;
            }
        }
        var str2 ='<p data-url="'+url+'" class="loadmore"><button class="btn btn-normal gray btn-bordered">更多文章</button></p>';
        $(".newsArticles .bd").after(str2);
        getUrl(url,index);
     }

     function getUrl(url,index){
        var loading = '<li><div class="loading">加载中...</div></li>'; 
        $(".newsArticles .bd ul:eq("+index+")").append(loading);
        // console.info('url',url)
        $.get(url,function(data){
          
        }, "json").error(function(data) {
            layer.msg("已经没有数据了！");
        }).success(function(data) {
            $(".loading").parent().remove();
            var obj = data.data;
            var str = "";
            // console.info(obj)
            if(obj.length > 0){
                for(var i=0;i<obj.length; i++){
                  var time = obj[i].created_at;
                      // time = time.substr(0,16);
                    str+='<li>'
                        if(index == 0){
                            str+='<a href="'+jumpUrl+'/news/'+obj[i].id+'/view" title="'+obj[i].title+'" class="pics">'
                              str+='<img alt="" src="'+obj[i].img+'" width="200" height="150">'
                            str+='</a>'
                        }else if(index == 1){
                            str+='<a href="'+jumpUrl+'/case/'+obj[i].id+'/view" title="'+obj[i].title+'" class="pics">'
                              str+='<img alt="" src="'+obj[i].img+'" width="200" height="150">'
                            str+='</a>'
                        }else if(index == 2){
                            str+='<a href="'+jumpUrl+'/knowledge/article/'+obj[i].id+'/view" title="'+obj[i].title+'" class="pics">'
                              str+='<img alt="" src="'+obj[i].img+'" width="200" height="150">'
                            str+='</a>'
                        }

                        str+='<div class="cont">'
                          str+='<h4>'
                            if(index == 0){
                                str+='<a href="'+jumpUrl+'/news/'+obj[i].id+'/view" title="'+obj[i].title+'" class="title">'+obj[i].title+'</a>'
                            }else if(index == 1){
                                str+='<a href="'+jumpUrl+'/case/'+obj[i].id+'/view" title="'+obj[i].title+'" class="title">'+obj[i].title+'</a>'
                            }else if(index == 2){
                                str+='<a href="'+jumpUrl+'/knowledge/article/'+obj[i].id+'/view" title="'+obj[i].title+'" class="title">'+obj[i].title+'</a>'
                            }

                          str+='</h4>'
                          str+='<div class="info">'
                            str+='<a title="律团" target="_blank" href="javascript:void(0);" class="name">律团</a>'
                            str+='<span class="gap-point">'+time+'</span>'
                          str+='</div>'
                          if (obj[i].info.length >48) {
                            str+='<p class="intro">'+obj[i].info.substr(0,45)+' ...</p>'
                          }else{
                          str+='<p class="intro">'+obj[i].info+'</p>'
                          };
                          str+='<div class="options">'
                            str+='<a href="javascript:void(0);" class="like js-like" title="点赞"  data-id="'+obj[i].id+'">'
                              str+='<span class="icon"><i class="icon-like"></i></span>'
                              str+='<span class="num">'+obj[i].likes_count+'</span>'
                            str+='</a>'
                            str+='<a class="comment like " title="评价数"  data-id="'+obj[i].id+'">'
                              str+='<span class="icon"><i class="icon-comment"></i></span>'
                              str+='<span class="num">'+obj[i].comments_count+'</span>'
                            str+='</a>'
                            str+='<a href="javascript:void(0);" class="favorite" data-id="'+obj[i].id+'" title="收藏">'
                              if(obj[i].is_collect){
                                    str+='<span class="icon"><i class="icon-favorite-on"></i></span>'
                              }else{
                                  str+='<span class="icon"><i class="icon-favorite"></i></span>'
                              }
                              str+='<span class="num shu">'+obj[i].collects_count+'</span>'
                            str+='</a>'
                          str+='</div>'
                        str+='</div>'
                    str+='</li>';
                }
                
            }else{
                str+='<li class="nodata">';
                    str+='暂无数据！';
                str+='</li>';
            }

            $(".newsArticles .bd ul:eq("+index+")").append(str);

        })
     }

    //点赞
    $(document).on('click', '.js-like', function(e) {
      if(islogin == "" || islogin == null){
        layer.msg("请登陆后在操作！"); 
      }else{
        var item_id = $(this).attr("data-id");
        var token = $("input:[name='_token']").val();
        var params = {};
        params['item_type'] = 'article';
        params['item_id'] = item_id;
        params['_token'] = token;
        var url = jumpUrl+'/like';
        var obj = $(this).children('.num');
        $.post(url,params,function(data){
          }, "json").error(function(data) {
            var error =  $.parseJSON(data.responseText);
            layer.msg(error.error_messages.item_type[0]);
          }).success(function(data) { 
        	  obj.text(data.data['likes_count']);
            layer.msg('点赞成功');
          })
          
      }
      
      stopDefault(e);
    })

 //收藏文章

    $(document).on("click", ".favorite", function(e){
      if(islogin == "" || islogin == null){
      layer.msg("请登录后在收藏！");
      return false;
    }else{
      var $this = $(this);
      var url = jumpUrl+"/collect";
      var thisId = $this.attr("data-id");
      var _token = $("input[name='_token']").val();
      $.post(url,{'collect_type':'3', 'item_id':thisId, '_token':_token},function(){
        
      }, 'json').error(function(data){
            var obj = $.parseJSON(data.responseText);
            $.each(obj.error_messages, function(i, item){
              layer.msg(item[0]);
            });
      }).success(function(data){
        var obj = data.data;
          layer.msg('收藏成功');
          $this.children('.icon').children('.icon-favorite').attr("class","icon-favorite-on");
          $this.children('.shu').text(obj);
      });
      }
    });


  //点击关注
  $(document).on('click', '.attention', function(e) {
    
    if(islogin == "" || islogin == null){
      layer.msg("请登录后在关注！");
      return false;
    }else{
      var loading = layer.msg('操作进行中...', {icon: 16});
      var $this = $(this);
      var follow_id = $(this).attr("data-id");
      var params = {};
      params['follow_id'] = follow_id;
      params['user_id'] = user_id;
      var url = jumpUrl+'/follow';
      $.post(url,params,function(data){
        }, "json").error(function(data) {
          var error =  $.parseJSON(data.responseText);
          $.each(error.error_messages, function(i, item){
            layer.msg(item[0]);
          });

        }).success(function(data) {
          var obj = data.data;
          layer.msg(obj);
          $this.text("取消关注");
          $this.attr("class","unfollow");
          })
      }
      stopDefault(e);
    });
   //取消关注
  $(document).on('click', '.unfollow', function(e) {
    if(islogin == "" || islogin == null){
      layer.msg("请登录后在关注！");
      return false;
    }else{
      var loading = layer.msg('操作进行中...', {icon: 16});
      var $this = $(this);
      var follow_id = $(this).attr("data-id");
      var params = {};
      params['follow_id'] = follow_id;
      params['user_id'] = user_id;
      var url = jumpUrl+'/follow/remove';
      $.post(url,params,function(data){
        }, "json").error(function(data) {
          var error =  $.parseJSON(data.responseText);
          $.each(error.error_messages, function(i, item){
            layer.msg(item[0]);
          });

        }).success(function(data) {
          var obj = data.data;
          layer.msg(obj);
          $this.text("关  注");
          $this.attr("class","attention");
          })
      }
      stopDefault(e);
    });


});

